<script lang="ts" setup>
import PageDialog from "@/components/PageDialog/index.vue";
import {ref} from "vue";
import {editStation} from "@/api/demoApi";
import {message} from "@/utils/message";

defineOptions({
  name: "paymentDialog"
});

/**-------------------表单相关-------------------**/
//表单参数
const formData = ref({
  id: null,
  stationNo:null,
  planName:null,
  buildPayment:null,
  performance:null
})

//表单校验
const formRules = ref({
  stationNo:[{required:true,message:'请输入更新后编号',trigger:'blur'}],
  planName:[{required:true,message:'请输入规划站名',trigger:'blur'}],
})
/**-------------------打开弹框-------------------**/
//标题
const dialogTitle = ref('')
const dialog = ref()
//弹框显示
const dialogShow = (title: string,data: object)=>{
  dialogTitle.value = title
  formData.value = {...data}
  dialog.value.dialogVisible = true
}
defineExpose({
  dialogShow
})

/**-------------------事件回调-------------------**/
//取消
const form = ref()
const handlerCancel = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.resetFields()
    formData.value = {
      id: null,
      stationNo:null,
      planName:null,
      buildPayment:null,
      performance:null
    }
    dialog.value.dialogVisible = false
    resolve({})
  }catch(e){
    console.log(e)
    reject(e)
  }
})
//确认
const emits = defineEmits(['done'])
const handlerConfirm = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.validate()
    editStation(formData.value)
    message("登记成功", { type: "success" });
    await handlerCancel()
    emits('done')
    resolve({})
  }catch(e){
    reject(e)
  }
})
</script>

<template>
  <PageDialog ref="dialog" :handler-cancel="handlerCancel" :handler-confirm="handlerConfirm" :title="dialogTitle" width="55%">
    <el-form ref="form" :model="formData" :rules="formRules" label-position="top" label-width="auto">
      <el-form-item label="更新后编号" prop="stationNo">
        <el-input v-model="formData.stationNo" clearable disabled placeholder="请输入更新后编号" />
      </el-form-item>
      <el-form-item label="规划站名" prop="planName">
        <el-input v-model="formData.planName" clearable disabled placeholder="请输入规划站名" />
      </el-form-item>
      <el-form-item label="配套投入" prop="buildPayment">
        <el-input-number v-model.number="formData.buildPayment" :controls="false" :precision="2" :step="100" placeholder="请输入配套投入"/>
      </el-form-item>
      <el-form-item label="外部绩效" prop="performance">
        <el-input-number v-model.number="formData.performance" :controls="false" :precision="2" :step="100" placeholder="请输入外部绩效"/>
      </el-form-item>
    </el-form>
  </PageDialog>
</template>

<style lang="scss" scoped>
:deep(.el-form) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;

  .el-form-item {
    width: 48%;

    .el-input,
    .el-input-number,
    .el-select {
      width: 100%;
    }
  }
}
</style>
